<?php
/**
 * Created by PhpStorm.
 * User: lxq
 * Date: 2018/4/19
 * Time: 9:26
 */
class itineraryControl extends BaseHomeControl {

    public function __construct()
    {
        if($_GET['op']=='place_info'){
            $this->_must_login = true;
        }
        parent::__construct();
        Tpl::setDir('itinerary');
        Tpl::setLayout('new_layout');
    }

    /**
     * 列表页
     */
    public function indexOp()
    {
        /* @var itineraryModel $itinerary_model*/
        $itinerary_model = Model('itinerary');
        $area_model  = Model('area');
        $category_model = Model('ticket_category');
        if($_SESSION['location']){
            $location = $_SESSION['location'];
            $field = '*';
            //$distance = 'SQRT(pow('. $location['lat'] .' - hotel_lat, 2) + pow('. $location['lng'] .'  - hotel_lng, 2)) AS distance';
            $distance = 'SQRT(pow('. $location['lng'] .' - ir_lat, 2) + pow('. $location['lat'] .'  - ir_lng, 2)) AS distance';
            $field .= ','.$distance;
        }
        $condition = array();
        $condition['ir_status'] = 1;
        if($cate_id = $_GET['cate_id']){
            $condition['cate_id'] = intval($cate_id);//分类ID
        }
        if($area_id = $_GET['area']){
            $condition['ir_area_id'] = $area_id; //地区ID
        }
        if($keyword = trim($_GET['keyword'])){
            $condition['ir_title'] = array('like','%'.$keyword.'%');
        }
        if($order=$_GET['order']){
            //排序
            $order_arr = explode('|',$order);
            $order = $order_arr[0].' '.$order_arr[1];
        }
        $list = $itinerary_model->getItineraryListPage($condition,$field,5,$order);
        if($list && is_array($list)){
            foreach($list  as $key => $value){
                $list[$key]['ir_thumb'] = getGoodsImg($value['ir_thumb'],ITINERARY_MARK);
            }
        }
        Tpl::output('list',$list);
        //AJAX加载分页
        if(isset($_GET['isajax']) && $_GET['isajax'] == 1){
            Tpl::showpage('itinerary.index.page','null_layout');
            exit;
        }
        $cityID  = $itinerary_model->getItineraryAreaId();//地区列表
        if($cityID){
            $area_arr = array();
            foreach($cityID as $item =>$value){
                $cityID[$item]['name'] = $area_model->getfby_id($value['ir_area_id']);
            }
            Tpl::output('area_arr',$cityID);
        }
        //旅行社底下所有分类
        $cate = $category_model->getCategoryList(array('tc_pid'=>ITINERARY_MARK,'is_del'=>0));
        Tpl::output('TotalNum',pagecmd("gettotalnum"));
        Tpl::output('TotalPage',pagecmd("gettotalpage"));
        Tpl::output('cate',$cate);
        Tpl::showpage('itinerary.index');

    }

    /**
     * 详情
     */
    public function infoOp()
    {
        /* @var itineraryModel $itinerary_model*/
        $itinerary_model = Model('itinerary');
        $ir_id = intval($_GET['irid']);
        if($ir_id>0){
            $itinerary_info = $itinerary_model->getItineraryInfo(array('id'=>$ir_id));
            if(empty($itinerary_info)){
                showMessage('该商品不存在');
            }
            $itinerary_info['ir_thumb'] = getGoodsImg($itinerary_info['ir_thumb'],ITINERARY_MARK);
            /* @var ticket_imagesModel $images_model */
            $images_model = Model('ticket_images');
            //景点相册
            $images = $images_model->getTicketImageList(array('tk_id'=>$ir_id,'img_deleted'=>0,'type'=>ITINERARY_MARK));
            if($images && is_array($images)){
                foreach($images  as $image =>$vo){
                    $images[$image]['img_path'] = getGoodsImg($vo['img_path'],ITINERARY_MARK,'single');
                }
            }
            Tpl::output('images',$images);
            Tpl::output('itinerary_info',$itinerary_info);
            Tpl::showpage('itinerary_info');
        }
    }

    /**
     * 确认订单
     */
    public function place_infoOp()
    {
        $irid = intval($_GET['irid']);
        if($irid>0){
            /* @var itineraryModel $itinerary_model*/
            $itinerary_model = Model('itinerary');
            $itinerary_info = $itinerary_model->getItineraryInfo(array('id'=>$irid,'ir_status'=>1));

            if(empty($itinerary_info)){
                showMessage('商品不存在');
            }
            $itinerary_info['ir_thumb'] = getGoodsImg($itinerary_info['ir_thumb'],ITINERARY_MARK);
            Tpl::output('itinerary_info',$itinerary_info);
            Tpl::showpage('place_info');
        }
    }
}